package com.dcrzl.shcz.module.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.dcrzl.shcz.common.entity.info.Content;

import com.dcrzl.shcz.common.entity.info.Gov;
import com.dcrzl.shcz.common.mapper.info.ContentMapper;
import com.dcrzl.shcz.common.service.impl.BaseServiceImpl;
import com.dcrzl.shcz.common.utils.PageInfo;
import com.dcrzl.shcz.common.utils.Result;
import com.dcrzl.shcz.common.utils.SysConstant;
import com.dcrzl.shcz.module.info.service.ContentService;
import com.dcrzl.shcz.module.info.service.GovService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.*;

@Service("contentService")
public class ContentServiceImpl extends BaseServiceImpl<ContentMapper, Content> implements ContentService {

    @Autowired
    private GovService govService;

    private LambdaQueryWrapper<Content> commonWrapper(Content content) {
        LambdaQueryWrapper<Content> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(content.getId()),Content::getId,content.getId());
        wrapper.like(StringUtils.hasText(content.getTitle()),Content::getTitle,content.getTitle());
        wrapper.eq(Objects.nonNull(content.getReleaseStatus()),Content::getReleaseStatus,content.getReleaseStatus());
        wrapper.eq(Objects.nonNull(content.getGovType()),Content::getGovType,content.getGovType());
        wrapper.orderByAsc(Content::getOrderNum).orderByDesc(Content::getCreateTime).orderByDesc(Content::getId);
        return wrapper;
    }

    @Override
    public Result<Content> selectOne(Content content) {
        LambdaQueryWrapper<Content> wrapper = this.commonWrapper(content);
        return super.selectOne(wrapper);
    }

    @Override
    public Result<List<Content>> selectList(Content content) {
        LambdaQueryWrapper<Content> wrapper = this.commonWrapper(content);
        return super.selectList(wrapper);
    }

    @Override
    public PageInfo<Content> selectPage(Content content) {
        LambdaQueryWrapper<Content> wrapper = this.commonWrapper(content);
        return super.selectPage(content, wrapper);
    }

    @Override
    public PageInfo<Content> selectUPage(Content content) {
        PageInfo<Content> contentPageInfo = this.selectPage(content);
        List<Content> list = contentPageInfo.getList();
        Set<Long> govIdList = new HashSet<>();
        list.forEach(content1 -> {
            govIdList.add(content1.getGovType());
        });
        if(!CollectionUtils.isEmpty(govIdList)){
            Result<List<Gov>> listResult = govService.selectList(new Gov().setIdList(govIdList));
            if(listResult.isSuccess()){
                List<Gov> govList = listResult.getData();
                Map<Long,Gov> map  = new HashMap<>();
                govList.forEach(gov -> {
                    map.put(gov.getId(),gov);
                });
                list.forEach(content1 -> {
                    content1.setGov(map.get(content1.getGovType()));
                });
            }
        }
        return contentPageInfo;
    }

    @Override
    public Result<Integer> removeById(Serializable id) {
        Content content = new Content().setStatus(SysConstant.DATA_DELETE_STATUS)
                .setId((Long) id);
        return this.updateById(content);
    }

}